package com.ruoyi.web.controller.certificates;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TemporaryStaffAccountCertificate;
import com.ruoyi.system.service.TemporaryStaffAccountCertificateService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 临时施工人员证书信息(TemporaryStaffAccountCertificate)表控制层
 *
 * @author makejava
 * @since 2023-06-06 14:08:36
 */
@RestController
@RequestMapping("temporaryStaffAccountCertificate")
public class TemporaryStaffAccountCertificateController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TemporaryStaffAccountCertificateService temporaryStaffAccountCertificateService;

    /**
     * 分页查询
     *
     * @param temporaryStaffAccountCertificate 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        startPage();
        List<TemporaryStaffAccountCertificate> temporaryStaffAccountCertificates = temporaryStaffAccountCertificateService.queryAll(temporaryStaffAccountCertificate);
        return getDataTable(temporaryStaffAccountCertificates);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TemporaryStaffAccountCertificate> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.temporaryStaffAccountCertificateService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param temporaryStaffAccountCertificate 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity<TemporaryStaffAccountCertificate> add(@Validated @RequestBody TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        return ResponseEntity.ok(this.temporaryStaffAccountCertificateService.insert(temporaryStaffAccountCertificate));
    }

    /**
     * 编辑数据
     *
     * @param temporaryStaffAccountCertificate 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public ResponseEntity<TemporaryStaffAccountCertificate> edit(@Validated @RequestBody TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        return ResponseEntity.ok(this.temporaryStaffAccountCertificateService.update(temporaryStaffAccountCertificate));
    }

    /**
     * 删除数据
     *
     * @param temporaryStaffAccountCertificate 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        return ResponseEntity.ok(this.temporaryStaffAccountCertificateService.deleteById(temporaryStaffAccountCertificate.getId()));
    }

    /**
     * 新增数据
     *
     * @param temporaryStaffAccountCertificate 实体
     * @return 新增结果
     */
    @PostMapping("mateCertificate")
    public AjaxResult mateCertificate(@Validated @RequestBody TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        if (null == temporaryStaffAccountCertificate || CollectionUtils.isEmpty(temporaryStaffAccountCertificate.getIds())) {
            return AjaxResult.error("参数异常");
        }
        Map<Integer, List<TemporaryStaffAccountCertificate>> listMap = temporaryStaffAccountCertificateService.mateCertificate(temporaryStaffAccountCertificate);
        return AjaxResult.success(listMap);
    }

    @Log(title = "证书信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TemporaryStaffAccountCertificate> util = new ExcelUtil<>(TemporaryStaffAccountCertificate.class);
        List<TemporaryStaffAccountCertificate> temporaryStaffAccountCertificates = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = temporaryStaffAccountCertificateService.importExcel(temporaryStaffAccountCertificates, updateSupport, operName);
        return success(message);
    }

    @Log(title = "证书信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemporaryStaffAccountCertificate temporaryStaffAccountCertificate) {
        List<TemporaryStaffAccountCertificate> list = temporaryStaffAccountCertificateService.queryAll(temporaryStaffAccountCertificate);
        ExcelUtil<TemporaryStaffAccountCertificate> util = new ExcelUtil<>(TemporaryStaffAccountCertificate.class);
        util.exportExcel(response, list, "证书信息");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TemporaryStaffAccountCertificate> util = new ExcelUtil<>(TemporaryStaffAccountCertificate.class);
        util.importTemplateExcel(response, "证书信息");
    }

    @GetMapping("/sendTest")
    public void sendTest() {
        temporaryStaffAccountCertificateService.sendTemporaryStaffAccountCertificate();
    }

}

